/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package io.netopen.hotbitmapgg.ringprogressbar.slice;

import io.netopen.hotbitmapgg.library.RingProgressBar;
import io.netopen.hotbitmapgg.ringprogressbar.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.TextTool;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * MainAbilitySlice
 *
 * @since 2021/07/23
 */
public class MainAbilitySlice extends AbilitySlice {
    private RingProgressBar mRingProgressBar1;
    private RingProgressBar mRingProgressBar2;
    private int progress = 0;
    private Revocable revocable;
    private final EventRunner runner = EventRunner.getMainEventRunner();
    private final EventHandler myEventHandler = new EventHandler(runner) {
        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            setProgress(event);
        }
    };

    private void setProgress(InnerEvent event) {
        if (event.eventId == 0) {
            if (progress < 100) {
                progress++;
                mRingProgressBar1.setProgress(progress);
                mRingProgressBar2.setProgress(progress);
                mRingProgressBar1.setOnProgressListener(() -> {
                    // Here after the completion of the processing
                });
                mRingProgressBar2.setOnProgressListener(() -> {
                    // Here after the completion of the processing
                });
            }
        }
    }

    @Override
    public void onStart(Intent intent) {
        try {
            int color = getAbility().getResourceManager().getElement(ResourceTable.Color_colorPrimaryDark).getColor();
            getAbility().getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mRingProgressBar1 = (RingProgressBar) findComponentById(ResourceTable.Id_progress_bar_1);
        mRingProgressBar2 = (RingProgressBar) findComponentById(ResourceTable.Id_progress_bar_2);
        revocable = getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(100);
                    myEventHandler.sendEvent(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        revocable.revoke();
    }
    /**
     * 是否为空
     *
     * @param content content
     * @return boolean
     */
    public static boolean isEmpty(String content) {
        return TextTool.isNullOrEmpty(content);
    }

    /**
     * 是否相等
     *
     * @param string1 string1
     * @param string2 string2
     * @return boolean
     */
    public static boolean isSame(String string1, String string2) {
        return TextTool.isEqual(string1, string2);
    }
}
